package reduce.fz.com.reduce.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.StoryDetailAdapter;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.constant.ParamConst;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.init.RecycleOper;
import reduce.fz.com.reduce.service.StoryService;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.ToastUtil;

public class StoryDetailActivity extends BaseActivity implements View.OnClickListener {


    private Context context;
    private StoryCard storyCard;
    private Long storyid;
    private RecyclerView mDetailListView;
    private StoryDetailAdapter storyDetailAdapter;
    private StoryService storyService = ServiceBuilder.builderStoryService(null);

    private RelativeLayout mStoryUseFul;
    private RelativeLayout mStoryUseLess;
    private RelativeLayout mStoryComments;
    private LoadingDialog dialog = null;

    public void initStoryCard() {
        dialog = new LoadingDialog(this);
        dialog.show();
        storyService.loadStoryDetail(storyid, new AbResponseListener<StoryCard>(new TypeReference<Result<StoryCard>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<StoryCard> data) {
                dialog.dismiss();
                if (!(data.getData() instanceof StoryCard)) return;
                storyCard = data.getData();
                storyDetailAdapter.initData(storyCard);
                setCount(storyCard.storyAttach.count_userful, storyCard.storyAttach.count_useless, storyCard.storyAttach.count_reply);
            }
        });
    }


    public void read() {
        storyService.storyRead(storyid, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }) {
            @Override
            public void error(ServiceException e) {
            }

            @Override
            public void responseInfo(Result<HashMap> data) {
                System.out.print("...");
            }
        });
    }

    public void initData() {
        storyid = getIntent().getLongExtra("storyid", 0);
        read();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story_detail);
        initData();
        initWidget();
        initRecycle();
        initStoryCard();

    }

    public void setCount(long useful, long useless, long comments) {
        if (storyCard == null) return;
        TextView use = (TextView) findViewById(R.id.tv_story_useful);
        TextView unuser = (TextView) findViewById(R.id.tv_story_useless);
        TextView comm = (TextView) findViewById(R.id.tv_story_comments);

        use.setText("有用(" + useful + ")");
        unuser.setText("没用(" + useless + ")");
        comm.setText("评价(" + comments + ")");
    }


    public void initRecycle() {
        storyDetailAdapter = new StoryDetailAdapter(getBaseContext());
        RecycleOper oper = new RecycleOper(getBaseContext(), mDetailListView);
        oper.setDefault();
        mDetailListView.setAdapter(storyDetailAdapter);

    }

    @Override
    public int getContentRid() {
        return R.layout.activity_story_detail;
    }

    @Override
    public void initWidget() {
        mDetailListView = (RecyclerView) findViewById(R.id.story_detail_list);
        mStoryComments = (RelativeLayout) findViewById(R.id.rl_story_comments);
        mStoryUseFul = (RelativeLayout) findViewById(R.id.rl_story_useful);
        mStoryUseLess = (RelativeLayout) findViewById(R.id.rl_story_useless);
        mStoryComments.setOnClickListener(this);
        mStoryUseLess.setOnClickListener(this);
        mStoryUseFul.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_story_useful:
                onClickUseful();
                break;
            case R.id.rl_story_comments:
                storyComment();
                break;
            case R.id.rl_story_useless:
                onClickUseless();
                break;
            default:
                break;
        }
    }

    public void onClickUseful() {
        // if (storyCard.storyAttach.isAppraise) return;
        storyCard.storyAttach.isAppraise = true;
        storyService.storyUseFul(storyid, ParamConst.STORY_TYPE_USEFUL, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }
        ) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<HashMap> data) {
                setCount(++storyCard.storyAttach.count_userful, storyCard.storyAttach.count_useless, storyCard.storyAttach.count_reply);

            }
        });

    }


    public void onClickUseless() {
        // if (storyCard.storyAttach.isAppraise) return;
        storyCard.storyAttach.isAppraise = true;
        storyService.storyUseFul(storyid, ParamConst.STORY_TYPE_USELESS, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result data) {
                setCount(storyCard.storyAttach.count_userful, ++storyCard.storyAttach.count_useless, storyCard.storyAttach.count_reply);
            }
        });
    }


    public void storyComment() {
        Intent intent = new Intent(StoryDetailActivity.this, StoryReplyActivity.class);
        intent.putExtra("storyid", storyid);
        startActivity(intent);

    }

}
